package com.twinkle.controller.cluster;

import com.alibaba.fastjson.JSONObject;
import com.twinkle.model.*;
import com.twinkle.service.*;
import com.twinkle.service.impl.DataBaseServiceImpl;
import com.twinkle.service.impl.MetadataColumnServiceImpl;
import com.twinkle.utils.ShellUtil;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.ParseException;
import java.util.List;

/**
 * @title: KMeansController
 * @description: kmeans算法接口类
 * @author: Paul
 * @date: 2023/3/13 9:39
 */
@Controller
@RequestMapping("/algo/kmeans")
public class KMeansController {

    @Autowired
    private DataBaseServiceImpl dataBaseService;
    @Autowired
    private ClusterResultService algoResultService;
    @Autowired
    private ParamTrainResultService paramTrainResultService;
    @Autowired
    private MetadataColumnServiceImpl metadataColumnService;
    @Autowired
    ClusterParamService clusterParamService;
    @Value("${data.algo.path}")
    private String algoPath;

    @RequestMapping("/addpage")
    public String addKmeansModelPage(Model model){
        //获取数据源id
        List<DataBase> dataBases = dataBaseService.queryAll();
        model.addAttribute("dataBases", dataBases);
        List<String> tableList = metadataColumnService.getTableList(9);
        model.addAttribute("tableList", tableList);
        List<MetadataColumnEntity> metadataColumnEntities = metadataColumnService.getTableColumns(9, "data1");
        model.addAttribute("metadataColumnEntities", metadataColumnEntities);
        return "algo_plat/algo_cluster/kmeans";
    }

    @ResponseBody
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public String addKmeansModel(Model model, @RequestBody JSONObject jsonObject) throws ParseException {
        //生成模型id
        System.out.println("JSONObject : " + JSONObject.toJSONString(jsonObject));
        ClusterParamUI clusterParam = JSONObject.parseObject(JSONObject.toJSONString(jsonObject), ClusterParamUI.class);
        String modelId = System.currentTimeMillis()+"";
        clusterParam.setId(modelId);
        //保存模型参数
        clusterParamService.addKMeansParam(clusterParam);
        System.out.println(JSONObject.toJSONString(clusterParam));
        //调用聚类算法
        String shell = "python " + algoPath + "clusters/kmeans.py \"" + JSONObject.toJSONString(clusterParam).replaceAll("\"", "\\\\\"") + "\"";
        System.out.println(shell);
        ProcessResult processResult = ShellUtil.execShell(shell);
        JSONObject processResultJsonObject = JSONObject.parseObject(JSONObject.toJSONString(processResult));
        processResultJsonObject.put("modelId", modelId);
        System.out.println("processResult : " + JSONObject.toJSONString(processResultJsonObject));
        return JSONObject.toJSONString(processResultJsonObject);
    }

    @RequestMapping(value = "/result", method = RequestMethod.GET)
    public String getAlgoResult(Model model, @Param("modelId")String modelId) throws ParseException {
        ClusterResult algoResult = algoResultService.getClusterResultByModelId(modelId);
        model.addAttribute("algoResult", algoResult);
        return "algo_plat/algo_cluster/kmeans_result";
    }


    @ResponseBody
    @RequestMapping(value = "/paramtrain", method = RequestMethod.POST)
    public String addParamTrain(Model model, @RequestBody JSONObject jsonObject) throws ParseException {
        //生成模型id
        System.out.println("JSONObject : " + JSONObject.toJSONString(jsonObject));
        String modelId = System.currentTimeMillis()+"";
        jsonObject.put("id", modelId);
        //保存模型参数
        System.out.println(JSONObject.toJSONString(jsonObject));
        //调用聚类算法
        String shell = "python " + algoPath + "clusters/kmeans.py \"" + JSONObject.toJSONString(jsonObject).replaceAll("\"", "\\\\\"") + "\"";
        System.out.println(shell);
        ProcessResult processResult = ShellUtil.execShell(shell);
        JSONObject processResultJsonObject = JSONObject.parseObject(JSONObject.toJSONString(processResult));
        processResultJsonObject.put("modelId", modelId);
        System.out.println("processResult : " + JSONObject.toJSONString(processResultJsonObject));
        return JSONObject.toJSONString(processResultJsonObject);
    }

    @ResponseBody
    @RequestMapping(value = "/paramtrainresult", method = RequestMethod.GET)
    public String getParamTrainResult(Model model, @Param("modelId")String modelId) throws ParseException {
        ParamTrainResult paramTrainResult = paramTrainResultService.getParamTrainResultByModelId(modelId);
        return JSONObject.toJSONString(paramTrainResult);
    }
}
